#include "pstyledbutton.h"
#include <QtDebug>




PStyledButton::PStyledButton (QWidget* parent, QPixmap *pixmap)
: QPushButton (parent)
, m_state (0)
, m_adjustToPixmap(true)
{
 m_startState = 0;
 m_states.resize(7);
}



PStyledButton::~PStyledButton ()
{

}



bool PStyledButton::event( QEvent* e)
{
    qWarning("event");
    //QMouseEvent* m_ev = (QMouseEvent*)e;
//    qDebug("PStyledButton::event");
    static bool hover=false;
    switch (e->type ())
    {
    
     case QEvent::Paint: 
                        /*adjusting the size of the button to the first state pixamp*/
                        if(m_states.size() && isAdjustToPixmap())
                        if(!m_states[0].isNull())
                        if(geometry().width()!= m_states[state()].width() || geometry().height()!= m_states[state()].height())
                           setGeometry(geometry().x(),geometry().y(),m_states[state()].width(),m_states[state()].height());
                        paintEvent((QPaintEvent*)e);
                        e->accept();

                break;
     case QEvent::Leave: 
                QPushButton::event(e);
                if(isEnabled())
                {
                    if(isChecked() || state()== on_mouseOver)
                    {
                    setState(on);
                    }
                    if(state()== off_mouseOver)
                    {
                        setState(off);
                    }
                    repaint();
                }
                hover = false;
                break;
     case QEvent::Enter:
                QPushButton::event(e);
                if(isEnabled())
                {
                    if(isChecked()|| state()== on)
                    {
                        setState(on_mouseOver);
                    }
                    else
                        {
                            setState(off_mouseOver) ;
                        }
                    repaint();
                }
                hover = true;
                break;    
     case QEvent::MouseButtonPress:
                QPushButton::event(e);
                if(isEnabled())
                {
                    if(isCheckable())
                    {
                        if(isChecked())
                            setState(on_pressed);
                        else setState(off_pressed);
                    }
                    else {
                            if(state()==on_mouseOver)
                                setState(on_pressed);
                            else setState(off_pressed);
                        }
                }
                repaint();
                break;
     case  QEvent::MouseButtonRelease:
         if(isEnabled())
         {
               if(hover)
                {
                  if(m_state == (int)off_pressed) //toglled
                  {
                    setState(off_mouseOver);
                  }
                  else  {
			             setState(off_mouseOver);
			            }
                  repaint();
                }  
                if(!hover)
                {
                  if(isChecked() || on_mouseOver) //toglled
                   setState(on);
                  else setState(off) ;
                  repaint();
                }  
         }
		QPushButton::event(e);
                break;           
     default: QPushButton::event(e);
              break;                        

    }

    return false;
}





void PStyledButton::setOn (bool on)
{
   // printf("setOn");
//    qDebug("setOn");
//    QPushButton::setOn (on);
//
//    if (updateState ())
//        repaint ();
}



void PStyledButton::focusInEvent (QFocusEvent* event)
{
    //QPushButton::focusInEvent (event);
//    if (updateState ())
//        repaint ();
}



void PStyledButton::focusOutEvent (QFocusEvent* event)
{
   // QPushButton::focusOutEvent (event);
//    if (updateState ())
//        repaint ();
}


// Sets current state for the button
void PStyledButton::setState(int state)
{
    qWarning("setState");
	//qDebug("PStyledButton::setState %d",state);
 
     if(state< m_states.size())
      m_state = state;
     else qWarning("illeagal state:%d",state);
  //qDebug("m_state:%d",m_state);
	update();
}

void PStyledButton::replaceStatePixmap(int state,QPixmap pixmap)
{
    qWarning("replaceStatePixmap");
//  qDebug("PStyledButton::replaceState");
  if(state < m_states.size() && !pixmap.isNull())
   m_states[state] = pixmap;
  else qWarning("illeagal state %d",state);
}

// returns the value of m_state
int PStyledButton::state()
{
//	qDebug("PStyledButton::state");
  return m_state;
}

// Adds a new state
void PStyledButton::addState(QPixmap pixmap)
{
    qWarning("getStatePixmap");
//	qDebug("PStyledButton::addState");
  m_states.push_back(pixmap);
}

// No description
void PStyledButton::paintEvent(QPaintEvent *e)
{
    qWarning("getStatePixmap");
      QPainter _p;//(this);
      _p.begin(this);
      if(m_states.size()>0 && !m_states[state()].isNull())
      {
        _p.drawPixmap(0,0,m_states[state()]);
      }  
      else{
           qWarning("PStyledButton::paintEvent: null state pixmap, drawing defalut index:%d",state());
           _p.end();
           QPushButton::paintEvent(e);
          }
    _p.end(); 
}

void PStyledButton::setAdjustToPixmap(bool adjust)
{
// qDebug("PStyledButton::setAdjustToPixmap");
 m_adjustToPixmap = adjust;
}

bool PStyledButton::isAdjustToPixmap()
{
// qDebug("PStyledButton::isAdjustToPixmap");
 return m_adjustToPixmap;
}



/*!
    \fn PStyledButton::getStatePixmap(int state=0)
 */
QPixmap PStyledButton::getStatePixmap(int state)
{
    qWarning("getStatePixmap");
    //const QPixmap empty;
//    qDebug("PStyledButton::getStatePixmap: state: %d",state);
    //qDebug("m_states.size():%d",m_states.size());
    if(m_states.size()<1)
    {
//     qDebug("empty");
     return QPixmap();
    } 
    return m_states[state];
}


/*!
    \fn PStyledButton::setNumStates(unsigned int numStates)
 */
void PStyledButton::setNumStates(unsigned int numStates)
{
    qWarning("setNumStates");
//   qDebug("PStyledButton::setNumStates");
   if(numStates> m_states.size())
    m_states.resize(numStates);
}


/*!
    \fn PStyledButton::setState0pixmap(QPixmap pixmap)
 */
void PStyledButton::setFirstStatePixmap(QPixmap pixmap)
{
    qWarning("getFirstStatePixmap");
//    qDebug("PStyledButton::setFirstStatePixmap");
    //m_states[0] = pixmap;
    if(pixmap.isNull())qWarning("PStyledButton::setState0pixmap is null");
    replaceStatePixmap(0,pixmap);
    replaceStatePixmap(1,pixmap);
    replaceStatePixmap(2,pixmap);
    replaceStatePixmap(3,pixmap);
    replaceStatePixmap(4,pixmap);
    replaceStatePixmap(5,pixmap);
    replaceStatePixmap(6,pixmap);
}


/*!
    \fn PStyledButton::getFirstStatePixmap()
 */
QPixmap PStyledButton::getFirstStatePixmap()
{
    qWarning("getFirstStatePixmap");
    if(m_startState< numStates())
        return m_states[m_startState];
    else return m_states[0];
}


/*!
    \fn PStyledButton::numStates()
 */
unsigned int  PStyledButton::numStates()
{
    return m_states.size();
}

void PStyledButton::setMask(QPixmap maskPixmap)
{
// qDebug("PStyledButton::setMask");
 QPushButton::setMask(maskPixmap.mask());
 m_maskPixmap = maskPixmap;
// qDebug("end PStyledButton::setMask");
}

QPixmap PStyledButton::maskPixmap()
{
 return m_maskPixmap;
}

/*!
    \fn PStyledButton::setState(BUTTON_STATE state)
 */
void PStyledButton::setState(BUTTON_STATE state)
{
    qWarning("setState");
    setState((int) state);
}


/*!
    \fn PStyledButton::replaceStatePixmap(BUTTON_STATE state, QPixmap pixmap)
 */
void PStyledButton::replaceStatePixmap(BUTTON_STATE state, QPixmap pixmap)
{
    qWarning("replaceStatePixmap");
    replaceStatePixmap((int)state,pixmap);
}


/*!
    \fn PStyledButton::setDisabled(bool disabled)
 */
void PStyledButton::setDisabled(bool disabled)
{
    qWarning("setDisabled");
    if(disabled)
        this->setState(PStyledButton::disabled);
    else this->setState(PStyledButton::off);
    update();
    QPushButton::setDisabled(disabled);
}


/*!
    \fn PStyledButton::setEnabled(bool enabled)
 */
void PStyledButton::setEnabled(bool enabled)
{
    qWarning("setEnabled");
    if(enabled)
     this->setState(PStyledButton::off);
    else this->setState(PStyledButton::disabled);
    update();
    QPushButton::setEnabled(enabled);
}


/*!
    \fn PStyledButton::setStartState(unsigned int state)
 */
void PStyledButton::setStartState(unsigned int state)
{
    qWarning("setStartState");
    m_startState = state;
}


/*!
    \fn PStyledButton::firstState()
 */
unsigned int PStyledButton::startState()
{
    qWarning("PStyledButton::startState");
    return m_startState;
}
